<?php

class StaffController extends Zend_Controller_Action {
	
	/**
	 *
	 * @var Cms_Model_Staff
	 */
	protected $_staffModel;
	
	/**
	 *
	 * @var array
	 */
	protected $_forms = array ();
	
	/**
	 *
	 * @var Zend_Controller_Action_Helper_Url
	 */
	protected $_urlHelper;
	
	public function init() {
		$this->_staffModel = new Cms_Model_Staff ();
		
		$this->_urlHelper = $this->_helper->getHelper ( 'url' );
	}
	
	public function staffAction() {
		
		$action = $this->_getParam ( 'do' );
		
		switch ($action) {
			
			case 'insert' :
				
				$this->view->backUrl = $this->_urlHelper->url ( array ('do' => 'access' ), 'staffStaff', true );
				
				$this->view->staffForm = $this->_getStaffForm ();
				
				$request = $this->getRequest ();
				
				if ($request->isPost ()) {
					if (! $this->_forms ['staffForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savestaff' );
					}
					
					$authService = new Cms_Service_Authentication();
					$this->_staffModel->saveStaff ( $this->_forms ['staffForm']->getValues (), $authService->getIdentity()->fullname );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Staff has been successfully created!" );
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffStaff', true );
				}
				
				$this->_helper->viewRenderer ( 'savestaff' );
				break;
			
			case 'update' :
				
				$staffId = ( int ) $this->_getParam ( 'id' );
				
				$this->view->backUrl = (null !== $this->_helper->redirectUrl ()) ? $this->_helper->redirectUrl () : $this->_urlHelper->url ( array ('do' => 'access' ), 'staffStaff', true );
				
				// if staff id does not exists - redirect to staff access
				$staffInfo = $this->_staffModel->getStaffById ( $staffId );
				if ((null === $staffInfo)) {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffStaff', true );
				}
				
				$this->view->staffForm = $this->_getStaffForm ( 'update', $staffId );
				
				$request = $this->getRequest ();
				
				if ($request->isPost ()) {
					
					if (! $this->_forms ['staffForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savestaff' );
					}
					
					$authService = new Cms_Service_Authentication();
					$this->_staffModel->saveStaff ( $this->_forms ['staffForm']->getValues (), $authService->getIdentity()->fullname, $staffInfo );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Staff has been successfully updated!" );
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffStaff', true );
					}
				
				}
				
				$this->view->staffForm->populate ( $staffInfo->toArray () );
				
				$this->_helper->viewRenderer ( 'savestaff' );
				
				break;
			
			case 'delete' :
				
				if ($this->getRequest ()->isPost ()) {
					
					$staffs = $this->_getParam ( 'selected' );
					
					if ((! empty ( $staffs ) && (is_array ( $staffs )))) {
						$countError = 0;
						foreach ( $staffs as $staff ) {
							//check if staff id has been tied to a payroll
							if ($this->_staffModel->getPayrollByStaffId ( $staff )->count() > 0) {
								$countError += 1;
							} else {
								// delete staff
								$this->_staffModel->deleteStaff ( $this->_staffModel->getStaffById ( $staff ) );
							}
						}
						if ($countError > 0) {
							$this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->addMessage ( "Warning: $countError staff(s) profile cannot be deleted as it is currently tied to a payroll(s)!" );
						} else {
							$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Staff(s) profile has been successfully deleted!" );
						}
					}
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffStaff', true );
					}
				
				} else {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffStaff', true );
				}
				
				break;
			
			case 'access' :
			default :
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->hasMessages ()) {
					$this->view->returnMessageSuccess = $this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->getMessages ();
				}
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->hasMessages ()) {
					$this->view->returnMessageWarning = $this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->getMessages ();
				}
				
				$data = array ();
				$partHeadUrl = '';
				$partPaginationUrl = '';
				
				if ($this->_getParam ( 'sort' )) {
					$data ['sort'] = $this->_getParam ( 'sort' );
					$partPaginationUrl .= '&sort=' . $this->_getParam ( 'sort' );
					
					if ($this->_getParam ( 'order' )) {
						$data ['order'] = $this->_getParam ( 'order' );
						$partPaginationUrl .= '&order=' . $this->_getParam ( 'order' );
					} else {
						$data ['order'] = 'asc';
						$partPaginationUrl .= '&order=asc';
					}
				}
				
				$partHeadUrl .= ($this->_getParam ( 'order' ) == 'asc') ? '&order=desc' : '&order=asc';
				
				if ($this->_getParam ( 'page' )) {
					$data ['page'] = intval ( $this->_getParam ( 'page' ) );
					$partHeadUrl .= '&page=' . intval ( $this->_getParam ( 'page' ) );
				} else { // turn on pagination by default
					$data ['page'] = 1;
				}
				
				$this->view->data = $data;
				$this->view->partHeadUrl = $partHeadUrl;
				$this->view->partPaginationUrl = $partPaginationUrl;
				
				$this->view->staffs = ($this->_staffModel->getStaffs ( $data )->count () > 0) ? $this->_staffModel->getStaffs ( $data ) : null;
				$this->_helper->viewRenderer ( 'liststaff' );
				break;
		}
	
	}
	
	public function payrollAction() {
		
		$action = $this->_getParam ( 'do' );
		
		switch ($action) {
			
			case 'insert' :
				
				$this->view->backUrl = $this->_urlHelper->url ( array ('do' => 'access' ), 'staffPayroll', true );
				
				$this->view->payrollForm = $this->_getPayrollForm ();
				
				$request = $this->getRequest ();
				
				if ($request->isPost ()) {
					if (! $this->_forms ['payrollForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savepayroll' );
					}
					$authService = new Cms_Service_Authentication ();
					$this->_staffModel->savePayroll ( $this->_forms ['payrollForm']->getValues (), $authService->getIdentity ()->fullname );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Payroll has been successfully created!" );
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffPayroll', true );
				}
				
				$this->_helper->viewRenderer ( 'savepayroll' );
				break;
			
			case 'update' :
				
				$entryId = ( int ) $this->_getParam ( 'id' );
				
				$this->view->backUrl = (null !== $this->_helper->redirectUrl ()) ? $this->_helper->redirectUrl () : $this->_urlHelper->url ( array ('do' => 'access' ), 'staffPayroll', true );
				
				// if entry id does not exists - redirect to payroll access
				$payrollInfo = $this->_staffModel->getPayrollByEntryId ( $entryId );
				if ((null === $payrollInfo)) {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffPayroll', true );
				}
				
				$this->view->payrollForm = $this->_getPayrollForm ( 'update', $entryId );
				
				$request = $this->getRequest ();
				
				if ($request->isPost ()) {
					
					if (! $this->_forms ['payrollForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savepayroll' );
					}
					$authService = new Cms_Service_Authentication ();
					$this->_staffModel->savePayroll ( $this->_forms ['payrollForm']->getValues (), $authService->getIdentity ()->fullname, $payrollInfo );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Payroll has been successfully updated!" );
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffPayroll', true );
					}
				
				}
				
				$payrollInfo = $payrollInfo->toArray();
				 
				$mysqlDate = new Zend_Date($payrollInfo['datePaid'], 'yyyy.MM.dd');
				$payrollInfo['datePaid'] = $mysqlDate->toString('dd/MM/yyyy');
				
				$this->view->payrollForm->populate ( $payrollInfo );
				
				$this->_helper->viewRenderer ( 'savepayroll' );
				
				break;
			
			case 'delete' :
				
				if ($this->getRequest ()->isPost ()) {
					
					$payrolls = $this->_getParam ( 'selected' );
					
					if ((! empty ( $payrolls ) && (is_array ( $payrolls )))) {
						foreach ( $payrolls as $payroll ) {
							// delete payroll
							$this->_staffModel->deletePayroll ( $this->_staffModel->getPayrollByEntryId ( $payroll ) );
						}
						$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Payroll(s) has been successfully deleted!" );
					}
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffPayroll', true );
					}
				
				} else {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'staffPayroll', true );
				}
				
				break;
			
			case 'access' :
			default :
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->hasMessages ()) {
					$this->view->returnMessageSuccess = $this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->getMessages ();
				}
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->hasMessages ()) {
					$this->view->returnMessageWarning = $this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->getMessages ();
				}
				
				$data = array ();
				$partHeadUrl = '';
				$partPaginationUrl = '';
				
				if ($this->_getParam ( 'sort' )) {
					$data ['sort'] = $this->_getParam ( 'sort' );
					$partPaginationUrl .= '&sort=' . $this->_getParam ( 'sort' );
					
					if ($this->_getParam ( 'order' )) {
						$data ['order'] = $this->_getParam ( 'order' );
						$partPaginationUrl .= '&order=' . $this->_getParam ( 'order' );
					} else {
						$data ['order'] = 'asc';
						$partPaginationUrl .= '&order=asc';
					}
				}
				
				$partHeadUrl .= ($this->_getParam ( 'order' ) == 'asc') ? '&order=desc' : '&order=asc';
				
				if ($this->_getParam ( 'page' )) {
					$data ['page'] = intval ( $this->_getParam ( 'page' ) );
					$partHeadUrl .= '&page=' . intval ( $this->_getParam ( 'page' ) );
				} else { // turn on pagination by default
					$data ['page'] = 1;
				}
				
				
				if ($this->_getParam('search')) {
					$data['search'] = $this->_getParam('search');
					$partHeadUrl .= '&search=' . $this->_getParam('search');
					$partPaginationUrl .= '&search=' . $this->_getParam('search');
				}
				
				$this->view->data = $data;
				$this->view->partHeadUrl = $partHeadUrl;
				$this->view->partPaginationUrl = $partPaginationUrl;
				
				$this->view->payrolls = ($this->_staffModel->getPayrolls ( $data )->count () > 0) ? $this->_staffModel->getPayrolls ( $data ) : null;
				$this->_helper->viewRenderer ( 'listpayroll' );
				break;
		}
	
	}
	
	protected function _getStaffForm($formState = 'create', $staffId = null) {
		$this->_forms ['staffForm'] = $this->_staffModel->getForm ( 'staffStaffForm' );
		$this->_forms ['staffForm']->setMethod ( 'post' );
		
		if ($formState == 'update') {
			$this->_forms ['staffForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'update', 'id' => $staffId ), 'staffStaff', true ) . $this->_helper->redirectUrl ( true ) );
		} else {
			$this->_forms ['staffForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'insert' ), 'staffStaff', true ) . $this->_helper->redirectUrl ( true ) );
		}
		
		return $this->_forms ['staffForm'];
	}
	
	protected function _getPayrollForm($formState = 'create', $entryId = null) {
		$this->_forms ['payrollForm'] = $this->_staffModel->getForm ( 'staffPayrollForm' );
		$this->_forms ['payrollForm']->setMethod ( 'post' );
		
		if ($formState == 'update') {
			$this->_forms ['payrollForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'update', 'id' => $entryId ), 'staffPayroll', true ) . $this->_helper->redirectUrl ( true ) );
		} else {
			$this->_forms ['payrollForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'insert' ), 'staffPayroll', true ) . $this->_helper->redirectUrl ( true ) );
		}
		
		return $this->_forms ['payrollForm'];
	}

}